﻿using System;
using System.Data;
using System.Data.SqlClient;
using Arizu.DAL;
using Arizu.Entity;

namespace Arizu.Repository
{
    public class AirizuUser : BaseProvider
    {
        #region 创建实例
        private static volatile AirizuUser instance;
        private static object lockHelper = new object();

        private AirizuUser()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static AirizuUser CreateInstance()
        {
            if (instance == null)
            {
                lock (lockHelper)
                {
                    if (instance == null)
                    {
                        instance = new AirizuUser();
                    }
                }
            }
            return instance;
        }
        #endregion

        /// <summary>
        /// 插入User信息
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public long InsertUserInfo(UserEntity user)
        {
            string sql = "INSERT INTO PersonInfo (Xing,Ming,Email,MobileTelephone,Pass,Sex,School,Adress,Work,TelephoneBack,MyDesc,TravelPlans,Image, EmailNotification, SMSNotification) VALUES (@Xing,@Ming,@Email,@MobileTelephone,@Pass,@Sex,@School,@Adress,@Work,@TelephoneBack,@MyDesc,@TravelPlans,@Image, @EmailNotification, @SMSNotification)";

            #region parameters
            SqlParameter parm_Xing = new SqlParameter("@Xing", user.Xing);
            SqlParameter parm_Ming = new SqlParameter("@Ming", user.Ming);
            SqlParameter parm_Email = new SqlParameter("@Email", user.Email);
            SqlParameter parm_MobileTelephone = new SqlParameter("@MobileTelephone", user.MobileTelephone);
            SqlParameter parm_Pass = new SqlParameter("@Pass", user.Pass);
            SqlParameter parm_Sex = new SqlParameter("@Sex", user.Sex);
            SqlParameter parm_School = new SqlParameter("@School", user.School);
            SqlParameter parm_Adress = new SqlParameter("@Adress", user.Adress);
            SqlParameter parm_Work = new SqlParameter("@Work", user.Work);
            SqlParameter parm_TelephoneBack = new SqlParameter("@TelephoneBack", user.TelephoneBack);
            SqlParameter parm_MyDesc = new SqlParameter("@MyDesc", user.MyDesc);
            SqlParameter parm_TravelPlans = new SqlParameter("@TravelPlans", user.TravelPlans);
            SqlParameter parm_Image = new SqlParameter("@Image", user.Image);
            SqlParameter parm_EmailNotification = new SqlParameter("@EmailNotification", 1111111111);
            SqlParameter parm_SMSNotification = new SqlParameter("@SMSNotification", 11111);

            SqlParameter[] parameters = { parm_Xing, parm_Ming, parm_Email, parm_MobileTelephone, parm_Pass, parm_Sex, parm_School, parm_Adress, parm_Work, parm_TelephoneBack, parm_MyDesc, parm_TravelPlans, parm_Image, parm_EmailNotification, parm_SMSNotification };
            #endregion

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
            return user.UserId;
        }

        /// <summary>
        /// 根据邮件地址获取User信息
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public DataTable GetUserByEmail(string email)
        {
            DataTable dt = new DataTable();
            string sql = "select * from PersonInfo where email='" + email + "'";

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
            return dt; 
        }

        /// <summary>
        /// 根据ID查询User信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetUser(long id)
        {
            DataTable dt = new DataTable();
            string sql = "select * from PersonInfo where personId=" + id;

            try
            {
                dt = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
            }
            catch (Exception)
            {

                throw;
            }
            return dt;
        }

        /// <summary>
        /// 更新用户照片文件名
        /// </summary>
        /// <param name="personId"></param>
        /// <param name="imgName"></param>
        public void UpdateImgName(int personId, string imgName)
        {
            DataTable dt = new DataTable();
            string sql = "update PersonInfo set Image='" + imgName + "' where PersonId=" + personId + "";

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="newPassword"></param>
        public void ChangePassword(long userId, string newPassword)
        {
            DataTable dt = new DataTable();
            string sql = "update PersonInfo set Pass='" + newPassword + "' where PersonId=" + userId + "";

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="user"></param>
        public void UpdateUserInfo(UserEntity user)
        {
            string sql = "Update PersonInfo Set Xing=@Xing, Ming=@Ming,Email=@Email,MobileTelephone=@MobileTelephone,Sex=@Sex,School=@School,Adress=@Adress,Work=@Work,TelephoneBack=@TelephoneBack,MyDesc=@MyDesc,TravelPlans=@TravelPlans where personId=@PersonId";

            #region parameters
            SqlParameter parm_Xing = new SqlParameter("@Xing", user.Xing);
            SqlParameter parm_Ming = new SqlParameter("@Ming", user.Ming);
            SqlParameter parm_Email = new SqlParameter("@Email", user.Email);
            SqlParameter parm_MobileTelephone = new SqlParameter("@MobileTelephone", user.MobileTelephone);
            //SqlParameter parm_Pass = new SqlParameter("@Pass", user.Pass);
            SqlParameter parm_Sex = new SqlParameter("@Sex", user.Sex);
            SqlParameter parm_School = new SqlParameter("@School", user.School);
            SqlParameter parm_Adress = new SqlParameter("@Adress", user.Adress);
            SqlParameter parm_Work = new SqlParameter("@Work", user.Work);
            SqlParameter parm_TelephoneBack = new SqlParameter("@TelephoneBack", user.TelephoneBack);
            SqlParameter parm_MyDesc = new SqlParameter("@MyDesc", user.MyDesc);
            SqlParameter parm_TravelPlans = new SqlParameter("@TravelPlans", user.TravelPlans);
            SqlParameter parm_Image = new SqlParameter("@Image", user.Image);
            SqlParameter parm_PersonId = new SqlParameter("@PersonId", user.UserId);
            //SqlParameter parm_OperaterId = new SqlParameter("@OperaterId", entity.OperaterId);

            SqlParameter[] parameters = { parm_Xing, parm_Ming, parm_Email, parm_MobileTelephone, parm_Sex, parm_School, parm_Adress, parm_Work, parm_TelephoneBack, parm_MyDesc, parm_TravelPlans, parm_Image, parm_PersonId };
            #endregion

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);

                //user.UserId = ds.Tables[0].Rows[0][0].ToString();

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 更新用户提醒设置
        /// </summary>
        /// <param name="user"></param>
        public void UpdateUserNotificationInfo(UserEntity user)
        {
            string sql = "Update PersonInfo Set EmailNotification=@EmailNotification, SMSNotification=@SMSNotification where personId=@PersonId";

            #region parameters
            SqlParameter parm_EmailNotification = new SqlParameter("@EmailNotification", user.EmailNotification);
            SqlParameter parm_SMSNotification = new SqlParameter("@SMSNotification", user.SMSNotification);
            SqlParameter parm_PersonId = new SqlParameter("@PersonId", user.UserId);
            SqlParameter[] parameters = { parm_EmailNotification, parm_SMSNotification, parm_PersonId };
            #endregion

            try
            {
                DataSet ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql, parameters);

            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }
    }
}
